package com.eastcom.collection.tools;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import java.io.*;
import java.sql.Timestamp
import java.sql.Types
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

public class TaskUtil<T> {
    private static Log logger = LogFactory.getLog(TaskUtil.class);

    public void save(String path, T javaObject) {
        FileOutputStream fos = null;
        ObjectOutputStream oos = null;
        File f = new File(path);
        try {
            fos = new FileOutputStream(f);
            oos = new ObjectOutputStream(fos);
            oos.writeObject(javaObject);    //括号内参数为要保存java对象
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                oos.close();
                fos.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    public T open(String path) {
        FileInputStream fis = null;
        ObjectInputStream ois = null;
        File f = new File(path);
        try {
            fis = new FileInputStream(f);
            ois = new ObjectInputStream(fis);
            T object = (T) ois.readObject();//强制类型转换
            return object;
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } finally {
            try {
                ois.close();
                fis.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return null;
    }

    /**
     * @param cmmand : shell 命令  如"ls" 或"ls -1";
     * @param directory :命令执行路径  如"/" ;
     * @return
     */
    public static String shellExecute(String[] cmmand, String directory) {
        String result = "";
        try {
            ProcessBuilder builder = new ProcessBuilder(cmmand);
            if (directory != null)
                builder.directory(new File(directory));
            builder.redirectErrorStream(true);
            Process process = builder.start();
            //得到命令执行后的结果
            InputStream is = process.getInputStream();
            byte[] buffer = new byte[1024];
            while (is.read(buffer) != -1) {
                result = result + new String(buffer);
            }
            is.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

    public static List<Object> fillInsertData(List<Object> rawDataList, List<Integer> dstSqlDataType,List<String> timeFormatList) {
        int i = 0
        int size = 0
        List<Object> dstDataList = new ArrayList<>()
        if(timeFormatList != null){
            size = timeFormatList.size()
            logger.debug("timeFormatSize="+size)
        }
        try {
            rawDataList.each {
                Object value = (String)it
//                println(value)
                Integer type = dstSqlDataType.get(i)
                i++
                println("type = "+type)
                switch (type) {
                    case Types.TIMESTAMP:
                        //目前多个regex只支持一种时间格式
                        int id = 0;
                        java.util.Date date = null;
                        Timestamp time
                        for(int j = 0; j < timeFormatList.size(); j++) {
                            try {
                                SimpleDateFormat simpleDateFormat = new SimpleDateFormat(timeFormatList.get(j));
                                date = simpleDateFormat.parse(value)
                                println(date)
                                break;
                            } catch (Exception e) {
                                // TODO: handle exception
                                println("时间转换错误！时间："+value+"格式："+timeFormatList.get(j))
                                continue
                            }
                        }


                        logger.debug(date.toLocaleString());
                        SimpleDateFormat simpleDateFormat2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                        String string = simpleDateFormat2.format(date);
                        logger.debug(string);
                        Timestamp timestamp = Timestamp.valueOf(string);
                        dstDataList.add(timestamp);
                        break;
                    case Types.VARCHAR:
                    case Types.NVARCHAR:
                        dstDataList.add(value)
                        break;
                    case Types.DECIMAL:
                        dstDataList.add(BigDecimal.valueOf(Double.valueOf(value)))
                        break;
                    case Types.NUMERIC:
                        dstDataList.add(BigDecimal.valueOf(Double.valueOf(value)))
                        break;
                    case Types.INTEGER:
                        dstDataList.add(Integer.valueOf(value))
                        break;
                    case Types.DOUBLE:
                        dstDataList.add(Double.valueOf(value))
                    default:
                        break;
                }
            }

        }catch (Exception e){
            e.printStackTrace()
        }
        return dstDataList
    }
}
